bump product version to 5.0.4.1
[LibreOffice.git] / shell / source / sessioninstall / SyncDbusSessionHelper.cxx
blob8f0a865a5b002d179b9b372b4b30ea4657d92b4e
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 <gio/gio.h>
13 #include <memory>
14 #include <vector>
16 using namespace ::com::sun::star::lang;
17 using namespace ::com::sun::star::uno;
19 namespace
21 struct GVariantDeleter { void operator()(GVariant* pV) { g_variant_unref(pV); } };
22 struct GVariantBuilderDeleter { void operator()(GVariantBuilder* pVB) { g_variant_builder_unref(pVB); } };
23 template <typename T> struct GObjectDeleter { void operator()(T* pO) { g_object_unref(pO); } };
24 class GErrorWrapper
26 GError* m_pError;
27 public:
28 GErrorWrapper(GError* pError) : m_pError(pError) {}
29 ~GErrorWrapper()
31 if(!m_pError)
32 return;
33 OUString sMsg = OUString::createFromAscii(m_pError->message);
34 g_error_free(m_pError);
35 throw RuntimeException(sMsg);
37 GError*& getRef() { return m_pError; }
39 static inline GDBusProxy* lcl_GetPackageKitProxy(const OUString& sInterface)
41 const OString sFullInterface = OUStringToOString("org.freedesktop.PackageKit." + sInterface, RTL_TEXTENCODING_ASCII_US);
42 GErrorWrapper error(NULL);
43 GDBusProxy* proxy = NULL;
44 proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
45 G_DBUS_PROXY_FLAGS_NONE, NULL,
46 "org.freedesktop.PackageKit",
47 "/org/freedesktop/PackageKit",
48 reinterpret_cast<const gchar*>(sFullInterface.getStr()),
49 NULL,
50 &error.getRef());
51 if(!proxy)
52 throw RuntimeException("couldnt get a proxy!");
53 return proxy;
56 void request(
57 char const * method, sal_uInt32 xid,
58 css::uno::Sequence<OUString> const & resources,
59 OUString const & interaction)
61 std::vector<OString> resUtf8;
62 std::shared_ptr<GVariantBuilder> builder(
63 g_variant_builder_new(G_VARIANT_TYPE ("as")), GVariantBuilderDeleter());
64 for (auto & i: resources) {
65 auto s(OUStringToOString(i, RTL_TEXTENCODING_UTF8));
66 resUtf8.push_back(s);
67 g_variant_builder_add(builder.get(), "s", s.getStr());
69 auto iactUtf8(OUStringToOString(interaction, RTL_TEXTENCODING_UTF8));
70 std::shared_ptr<GDBusProxy> proxy(
71 lcl_GetPackageKitProxy("Modify"), GObjectDeleter<GDBusProxy>());
72 GErrorWrapper error(nullptr);
73 g_dbus_proxy_call_sync(
74 proxy.get(), method,
75 g_variant_new(
76 "(uass)", static_cast<guint32>(xid), builder.get(),
77 iactUtf8.getStr()),
78 G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error.getRef());
83 namespace shell { namespace sessioninstall
85 SyncDbusSessionHelper::SyncDbusSessionHelper(Reference<XComponentContext> const&)
87 #if !GLIB_CHECK_VERSION(2,36,0)
88 g_type_init ();
89 #endif
92 void SyncDbusSessionHelper::InstallPackageFiles(
93 sal_uInt32 xid, css::uno::Sequence<OUString> const & files,
94 OUString const & interaction)
95 throw (css::uno::RuntimeException, std::exception)
97 request("InstallPackageFiles", xid, files, interaction);
100 void SyncDbusSessionHelper::InstallProvideFiles(
101 sal_uInt32 xid, css::uno::Sequence<OUString> const & files,
102 OUString const & interaction)
103 throw (css::uno::RuntimeException, std::exception)
105 request("InstallProvideFiles", xid, files, interaction);
108 void SyncDbusSessionHelper::InstallCatalogs(
109 sal_uInt32 xid, css::uno::Sequence<OUString> const & files,
110 OUString const & interaction)
111 throw (css::uno::RuntimeException, std::exception)
113 request("InstallCatalogs", xid, files, interaction);
116 void SyncDbusSessionHelper::InstallPackageNames(
117 sal_uInt32 xid, css::uno::Sequence<OUString> const & packages,
118 OUString const & interaction)
119 throw (css::uno::RuntimeException, std::exception)
121 request("InstallPackageNames", xid, packages, interaction);
124 void SyncDbusSessionHelper::InstallMimeTypes(
125 sal_uInt32 xid, css::uno::Sequence<OUString> const & mimeTypes,
126 OUString const & interaction)
127 throw (css::uno::RuntimeException, std::exception)
129 request("InstallMimeTypes", xid, mimeTypes, interaction);
132 void SyncDbusSessionHelper::InstallFontconfigResources(
133 sal_uInt32 xid, css::uno::Sequence<OUString> const & resources,
134 OUString const & interaction)
135 throw (css::uno::RuntimeException, std::exception)
137 request("InstallFontconfigResources", xid, resources, interaction);
140 void SyncDbusSessionHelper::InstallGStreamerResources(
141 sal_uInt32 xid, css::uno::Sequence<OUString> const & resources,
142 OUString const & interaction)
143 throw (css::uno::RuntimeException, std::exception)
145 request("InstallGStreamerResources", xid, resources, interaction);
148 void SyncDbusSessionHelper::RemovePackageByFiles(
149 sal_uInt32 xid, css::uno::Sequence<OUString> const & files,
150 OUString const & interaction)
151 throw (css::uno::RuntimeException, std::exception)
153 request("RemovePackageByFiles", xid, files, interaction);
156 void SyncDbusSessionHelper::InstallPrinterDrivers(
157 sal_uInt32 xid, css::uno::Sequence<OUString> const & files,
158 OUString const & interaction)
159 throw (css::uno::RuntimeException, std::exception)
161 request("InstallPrinteDrivers", xid, files, interaction);
164 void SAL_CALL SyncDbusSessionHelper::IsInstalled( const OUString& sPackagename, const OUString& sInteraction, sal_Bool& o_isInstalled ) throw (RuntimeException, std::exception)
166 const OString sPackagenameAscii = OUStringToOString(sPackagename, RTL_TEXTENCODING_ASCII_US);
167 const OString sInteractionAscii = OUStringToOString(sInteraction, RTL_TEXTENCODING_ASCII_US);
168 std::shared_ptr<GDBusProxy> proxy(lcl_GetPackageKitProxy("Query"), GObjectDeleter<GDBusProxy>());
169 GErrorWrapper error(NULL);
170 std::shared_ptr<GVariant> result(g_dbus_proxy_call_sync (proxy.get(),
171 "IsInstalled",
172 g_variant_new ("(ss)",
173 sPackagenameAscii.getStr(),
174 sInteractionAscii.getStr()),
175 G_DBUS_CALL_FLAGS_NONE,
176 -1, /* timeout */
177 NULL, /* cancellable */
178 &error.getRef()),GVariantDeleter());
179 if(result.get())
180 o_isInstalled = g_variant_get_boolean(g_variant_get_child_value(result.get(),0)) ? sal_True : sal_False;
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */