tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / shell / source / sessioninstall / SyncDbusSessionHelper.cxx
blob16eefa56af425c84e7dac850a11fda9dcf7211bb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include "SyncDbusSessionHelper.hxx"
12 #include <cppuhelper/supportsservice.hxx>
13 #include <gio/gio.h>
14 #include <cstring>
15 #include <memory>
16 #include <string_view>
17 #include <vector>
19 using namespace ::com::sun::star::lang;
20 using namespace ::com::sun::star::uno;
22 namespace
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
29 GError* m_pError;
30 public:
31 explicit GErrorWrapper() : m_pError(nullptr) {}
32 ~GErrorWrapper() noexcept(false)
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);
41 GError*& getRef() { return m_pError; }
43 GDBusProxy* lcl_GetPackageKitProxy(std::u16string_view sInterface)
45 const OString sFullInterface = "org.freedesktop.PackageKit." + OUStringToOString(sInterface, RTL_TEXTENCODING_ASCII_US);
46 GDBusProxy* proxy = nullptr;
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());
57 if(!proxy)
58 throw RuntimeException(u"couldn't get a proxy!"_ustr);
59 return proxy;
62 GVariant* pk_make_platform_data()
64 GVariantBuilder builder;
65 g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
66 return g_variant_builder_end(&builder);
69 void request(
70 char const * method,
71 css::uno::Sequence<OUString> const & resources,
72 std::u16string_view interaction)
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());
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());
97 namespace shell::sessioninstall
99 SyncDbusSessionHelper::SyncDbusSessionHelper(Reference<XComponentContext> const&)
101 #if !GLIB_CHECK_VERSION(2,36,0)
102 g_type_init ();
103 #endif
106 Sequence< OUString > SAL_CALL SyncDbusSessionHelper::getSupportedServiceNames()
108 return { u"org.freedesktop.PackageKit.SyncDbusSessionHelper"_ustr };
111 OUString SAL_CALL SyncDbusSessionHelper::getImplementationName()
113 return u"org.libreoffice.comp.shell.sessioninstall.SyncDbusSessionHelper"_ustr;
116 sal_Bool SAL_CALL SyncDbusSessionHelper::supportsService(const OUString& aServiceName)
118 return cppu::supportsService(this, aServiceName);
121 void SyncDbusSessionHelper::InstallPackageFiles(
122 css::uno::Sequence<OUString> const & files,
123 OUString const & interaction)
125 request("InstallPackageFiles", files, interaction);
128 void SyncDbusSessionHelper::InstallProvideFiles(
129 css::uno::Sequence<OUString> const & files,
130 OUString const & interaction)
132 request("InstallProvideFiles", files, interaction);
135 void SyncDbusSessionHelper::InstallCatalogs(
136 css::uno::Sequence<OUString> const & files,
137 OUString const & interaction)
139 request("InstallCatalogs", files, interaction);
142 void SyncDbusSessionHelper::InstallPackageNames(
143 css::uno::Sequence<OUString> const & packages,
144 OUString const & interaction)
146 request("InstallPackageNames", packages, interaction);
149 void SyncDbusSessionHelper::InstallMimeTypes(
150 css::uno::Sequence<OUString> const & mimeTypes,
151 OUString const & interaction)
153 request("InstallMimeTypes", mimeTypes, interaction);
156 void SyncDbusSessionHelper::InstallFontconfigResources(
157 css::uno::Sequence<OUString> const & resources,
158 OUString const & interaction)
160 request("InstallFontconfigResources", resources, interaction);
163 void SyncDbusSessionHelper::InstallGStreamerResources(
164 css::uno::Sequence<OUString> const & resources,
165 OUString const & interaction)
167 request("InstallGStreamerResources", resources, interaction);
170 void SyncDbusSessionHelper::RemovePackageByFiles(
171 css::uno::Sequence<OUString> const & files,
172 OUString const & interaction)
174 request("RemovePackageByFiles", files, interaction);
177 void SyncDbusSessionHelper::InstallPrinterDrivers(
178 css::uno::Sequence<OUString> const & files,
179 OUString const & interaction)
181 request("InstallPrinterDrivers", files, interaction);
184 void SAL_CALL SyncDbusSessionHelper::IsInstalled( const OUString& sPackagename, const OUString& sInteraction, sal_Bool& o_isInstalled )
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)));
203 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
204 shell_sessioninstall_get_implementation(
205 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
207 return cppu::acquire(new SyncDbusSessionHelper(context));
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */