| 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 |
|
| 10 |
#include "SyncDbusSessionHelper.hxx" |
| 11 |
|
| 12 |
#include <cppuhelper/supportsservice.hxx> |
| 13 |
#include <gio/gio.h> |
| 14 |
#include <cstring> |
| 15 |
#include <memory> |
| 16 |
#include <string_view> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
using namespace ::com::sun::star::lang; |
| 20 |
using namespace ::com::sun::star::uno; |
| 21 |
|
| 22 |
namespace |
| 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(u"couldn't get a proxy!"_ustr); |
| 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 |
|
| 69 |
void 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 |
|
| 97 |
namespace shell::sessioninstall |
| 98 |
{ |
| 99 |
SyncDbusSessionHelper::SyncDbusSessionHelper(Reference<XComponentContext> const&) |
| 100 |
{ |
| 101 |
#if !GLIB_CHECK_VERSION(2,36,0) |
| 102 |
g_type_init (); |
| 103 |
#endif |
| 104 |
} |
| 105 |
|
| 106 |
Sequence< OUString > SAL_CALL SyncDbusSessionHelper::getSupportedServiceNames() |
| 107 |
{ |
| 108 |
return { u"org.freedesktop.PackageKit.SyncDbusSessionHelper"_ustr }; |
| 109 |
} |
| 110 |
|
| 111 |
OUString SAL_CALL SyncDbusSessionHelper::getImplementationName() |
| 112 |
{ |
| 113 |
return u"org.libreoffice.comp.shell.sessioninstall.SyncDbusSessionHelper"_ustr; |
| 114 |
} |
| 115 |
|
| 116 |
sal_Bool SAL_CALL SyncDbusSessionHelper::supportsService(const OUString& aServiceName) |
| 117 |
{ |
| 118 |
return cppu::supportsService(this, aServiceName); |
| 119 |
} |
| 120 |
|
| 121 |
void SyncDbusSessionHelper::InstallPackageFiles( |
| 122 |
css::uno::Sequence<OUString> const & files, |
| 123 |
OUString const & interaction) |
| 124 |
{ |
| 125 |
request("InstallPackageFiles", files, interaction); |
| 126 |
} |
| 127 |
|
| 128 |
void SyncDbusSessionHelper::InstallProvideFiles( |
| 129 |
css::uno::Sequence<OUString> const & files, |
| 130 |
OUString const & interaction) |
| 131 |
{ |
| 132 |
request("InstallProvideFiles", files, interaction); |
| 133 |
} |
| 134 |
|
| 135 |
void SyncDbusSessionHelper::InstallCatalogs( |
| 136 |
css::uno::Sequence<OUString> const & files, |
| 137 |
OUString const & interaction) |
| 138 |
{ |
| 139 |
request("InstallCatalogs", files, interaction); |
| 140 |
} |
| 141 |
|
| 142 |
void SyncDbusSessionHelper::InstallPackageNames( |
| 143 |
css::uno::Sequence<OUString> const & packages, |
| 144 |
OUString const & interaction) |
| 145 |
{ |
| 146 |
request("InstallPackageNames", packages, interaction); |
| 147 |
} |
| 148 |
|
| 149 |
void SyncDbusSessionHelper::InstallMimeTypes( |
| 150 |
css::uno::Sequence<OUString> const & mimeTypes, |
| 151 |
OUString const & interaction) |
| 152 |
{ |
| 153 |
request("InstallMimeTypes", mimeTypes, interaction); |
| 154 |
} |
| 155 |
|
| 156 |
void SyncDbusSessionHelper::InstallFontconfigResources( |
| 157 |
css::uno::Sequence<OUString> const & resources, |
| 158 |
OUString const & interaction) |
| 159 |
{ |
| 160 |
request("InstallFontconfigResources", resources, interaction); |
| 161 |
} |
| 162 |
|
| 163 |
void SyncDbusSessionHelper::InstallGStreamerResources( |
| 164 |
css::uno::Sequence<OUString> const & resources, |
| 165 |
OUString const & interaction) |
| 166 |
{ |
| 167 |
request("InstallGStreamerResources", resources, interaction); |
| 168 |
} |
| 169 |
|
| 170 |
void SyncDbusSessionHelper::RemovePackageByFiles( |
| 171 |
css::uno::Sequence<OUString> const & files, |
| 172 |
OUString const & interaction) |
| 173 |
{ |
| 174 |
request("RemovePackageByFiles", files, interaction); |
| 175 |
} |
| 176 |
|
| 177 |
void SyncDbusSessionHelper::InstallPrinterDrivers( |
| 178 |
css::uno::Sequence<OUString> const & files, |
| 179 |
OUString const & interaction) |
| 180 |
{ |
| 181 |
request("InstallPrinterDrivers", files, interaction); |
| 182 |
} |
| 183 |
|
| 184 |
void 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 |
|
| 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&) |
| 206 |
{ |
| 207 |
return cppu::acquire(new SyncDbusSessionHelper(context)); |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |