LibreOffice Module shell (master) 1
SyncDbusSessionHelper.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
11
13#include <gio/gio.h>
14#include <cstring>
15#include <memory>
16#include <string_view>
17#include <vector>
18
19using namespace ::com::sun::star::lang;
20using namespace ::com::sun::star::uno;
21
22namespace
23{
24 struct GVariantDeleter { void operator()(GVariant* pV) { if (pV) g_variant_unref(pV); } };
25 struct GVariantBuilderDeleter { void operator()(GVariantBuilder* pVB) { g_variant_builder_unref(pVB); } };
26 template <typename T> struct GObjectDeleter { void operator()(T* pO) { g_object_unref(pO); } };
27 class GErrorWrapper
28 {
29 GError* m_pError;
30 public:
31 explicit GErrorWrapper() : m_pError(nullptr) {}
32 ~GErrorWrapper() noexcept(false)
33 {
34 if(!m_pError)
35 return;
36 OUString sMsg(
37 m_pError->message, std::strlen(m_pError->message), RTL_TEXTENCODING_UTF8);
38 g_error_free(m_pError);
39 throw RuntimeException(sMsg);
40 }
41 GError*& getRef() { return m_pError; }
42 };
43 GDBusProxy* lcl_GetPackageKitProxy(std::u16string_view sInterface)
44 {
45 const OString sFullInterface = "org.freedesktop.PackageKit." + OUStringToOString(sInterface, RTL_TEXTENCODING_ASCII_US);
46 GDBusProxy* proxy = nullptr;
47 {
48 GErrorWrapper error;
49 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
50 G_DBUS_PROXY_FLAGS_NONE, nullptr,
51 "org.freedesktop.PackageKit",
52 "/org/freedesktop/PackageKit",
53 reinterpret_cast<const gchar*>(sFullInterface.getStr()),
54 nullptr,
55 &error.getRef());
56 }
57 if(!proxy)
58 throw RuntimeException("couldn't get a proxy!");
59 return proxy;
60 }
61
62 GVariant* pk_make_platform_data()
63 {
64 GVariantBuilder builder;
65 g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
66 return g_variant_builder_end(&builder);
67 }
68
69void request(
70 char const * method,
71 css::uno::Sequence<OUString> const & resources,
72 std::u16string_view interaction)
73{
74 // Keep strings alive until after call to g_dbus_proxy_call_sync
75 std::vector<OString> resUtf8;
76 std::shared_ptr<GVariantBuilder> builder(
77 g_variant_builder_new(G_VARIANT_TYPE ("as")), GVariantBuilderDeleter());
78 for (auto & i: resources) {
79 auto s(OUStringToOString(i, RTL_TEXTENCODING_UTF8));
80 resUtf8.push_back(s);
81 g_variant_builder_add(builder.get(), "s", s.getStr());
82 }
83 auto iactUtf8(OUStringToOString(interaction, RTL_TEXTENCODING_UTF8));
84 std::shared_ptr<GDBusProxy> proxy(
85 lcl_GetPackageKitProxy(u"Modify2"), GObjectDeleter<GDBusProxy>());
86 GErrorWrapper error;
87 std::shared_ptr<GVariant> result(g_dbus_proxy_call_sync(
88 proxy.get(), method,
89 g_variant_new(
90 "(asss@a{sv})", builder.get(), iactUtf8.getStr(),
91 "libreoffice-startcenter.desktop", pk_make_platform_data()),
92 G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error.getRef()), GVariantDeleter());
93}
94
95}
96
98{
99 SyncDbusSessionHelper::SyncDbusSessionHelper(Reference<XComponentContext> const&)
100 {
101#if !GLIB_CHECK_VERSION(2,36,0)
102 g_type_init ();
103#endif
104 }
105
107{
108 return { "org.freedesktop.PackageKit.SyncDbusSessionHelper" };
109}
110
112{
113 return "org.libreoffice.comp.shell.sessioninstall.SyncDbusSessionHelper";
114}
115
116sal_Bool SAL_CALL SyncDbusSessionHelper::supportsService(const OUString& aServiceName)
117{
118 return cppu::supportsService(this, aServiceName);
119}
120
122 css::uno::Sequence<OUString> const & files,
123 OUString const & interaction)
124{
125 request("InstallPackageFiles", files, interaction);
126}
127
129 css::uno::Sequence<OUString> const & files,
130 OUString const & interaction)
131{
132 request("InstallProvideFiles", files, interaction);
133}
134
136 css::uno::Sequence<OUString> const & files,
137 OUString const & interaction)
138{
139 request("InstallCatalogs", files, interaction);
140}
141
143 css::uno::Sequence<OUString> const & packages,
144 OUString const & interaction)
145{
146 request("InstallPackageNames", packages, interaction);
147}
148
150 css::uno::Sequence<OUString> const & mimeTypes,
151 OUString const & interaction)
152{
153 request("InstallMimeTypes", mimeTypes, interaction);
154}
155
157 css::uno::Sequence<OUString> const & resources,
158 OUString const & interaction)
159{
160 request("InstallFontconfigResources", resources, interaction);
161}
162
164 css::uno::Sequence<OUString> const & resources,
165 OUString const & interaction)
166{
167 request("InstallGStreamerResources", resources, interaction);
168}
169
171 css::uno::Sequence<OUString> const & files,
172 OUString const & interaction)
173{
174 request("RemovePackageByFiles", files, interaction);
175}
176
178 css::uno::Sequence<OUString> const & files,
179 OUString const & interaction)
180{
181 request("InstallPrinterDrivers", files, interaction);
182}
183
184void SAL_CALL SyncDbusSessionHelper::IsInstalled( const OUString& sPackagename, const OUString& sInteraction, sal_Bool& o_isInstalled )
185{
186 const OString sPackagenameAscii = OUStringToOString(sPackagename, RTL_TEXTENCODING_ASCII_US);
187 const OString sInteractionAscii = OUStringToOString(sInteraction, RTL_TEXTENCODING_ASCII_US);
188 std::shared_ptr<GDBusProxy> proxy(lcl_GetPackageKitProxy(u"Query"), GObjectDeleter<GDBusProxy>());
189 GErrorWrapper error;
190 std::shared_ptr<GVariant> result(g_dbus_proxy_call_sync (proxy.get(),
191 "IsInstalled",
192 g_variant_new ("(ss)",
193 sPackagenameAscii.getStr(),
194 sInteractionAscii.getStr()),
195 G_DBUS_CALL_FLAGS_NONE,
196 -1, /* timeout */
197 nullptr, /* cancellable */
198 &error.getRef()),GVariantDeleter());
199 if(result)
200 o_isInstalled = bool(g_variant_get_boolean(g_variant_get_child_value(result.get(),0)));
201}
202
203extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
205 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
206{
207 return cppu::acquire(new SyncDbusSessionHelper(context));
208}
209
210}
211
212/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL InstallProvideFiles(const css::uno::Sequence< OUString > &files, const OUString &interaction) override
virtual void SAL_CALL InstallPrinterDrivers(const css::uno::Sequence< OUString > &files, const OUString &interaction) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void SAL_CALL InstallCatalogs(const css::uno::Sequence< OUString > &files, const OUString &interaction) override
SyncDbusSessionHelper(css::uno::Reference< css::uno::XComponentContext > const &)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL RemovePackageByFiles(const css::uno::Sequence< OUString > &files, const OUString &interaction) override
virtual void SAL_CALL InstallMimeTypes(const css::uno::Sequence< OUString > &mimeTypes, const OUString &interaction) override
virtual void SAL_CALL InstallFontconfigResources(const css::uno::Sequence< OUString > &resources, const OUString &interaction) override
virtual void SAL_CALL IsInstalled(const OUString &, const OUString &, sal_Bool &) override
virtual void SAL_CALL InstallPackageFiles(const css::uno::Sequence< OUString > &files, const OUString &interaction) override
virtual void SAL_CALL InstallGStreamerResources(const css::uno::Sequence< OUString > &resources, const OUString &interaction) override
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL InstallPackageNames(const css::uno::Sequence< OUString > &packages, const OUString &interaction) override
struct _GDBusProxy GDBusProxy
float u
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * shell_sessioninstall_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
unsigned char sal_Bool
Any result