1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <SyncDbusSessionHelper.hxx>
12 #include <comphelper/stlunosequence.hxx>
15 #include <boost/shared_ptr.hpp>
17 using namespace ::com::sun::star::lang
;
18 using namespace ::com::sun::star::uno
;
19 using namespace ::comphelper
;
20 using namespace ::std
;
21 using namespace ::rtl
;
25 struct GVariantDeleter
{ void operator()(GVariant
* pV
) { g_variant_unref(pV
); } };
26 struct GVariantBuilderDeleter
{ void operator()(GVariantBuilder
* pVB
) { g_variant_builder_unref(pVB
); } };
27 template <typename T
> struct GObjectDeleter
{ void operator()(T
* pO
) { g_object_unref(pO
); } };
32 GErrorWrapper(GError
* pError
) : m_pError(pError
) {}
37 OUString sMsg
= OUString::createFromAscii(m_pError
->message
);
38 g_error_free(m_pError
);
39 throw RuntimeException(sMsg
, NULL
);
41 GError
** getRef() { return &m_pError
; }
43 static inline GDBusProxy
* lcl_GetPackageKitProxy(const OUString sInterface
)
45 const OString sFullInterface
= OUStringToOString("org.freedesktop.PackageKit." + sInterface
, RTL_TEXTENCODING_ASCII_US
);
46 GErrorWrapper
error(NULL
);
47 GDBusProxy
* proxy
= NULL
;
48 proxy
= g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION
,
49 G_DBUS_PROXY_FLAGS_NONE
, NULL
,
50 "org.freedesktop.PackageKit",
51 "/org/freedesktop/PackageKit",
52 reinterpret_cast<const gchar
*>(sFullInterface
.getStr()),
56 throw RuntimeException(OUString("couldnt get a proxy!"),NULL
);
61 namespace shell
{ namespace sessioninstall
63 SyncDbusSessionHelper::SyncDbusSessionHelper(Reference
<XComponentContext
> const&)
65 #if !GLIB_CHECK_VERSION(2,36,0)
69 void SAL_CALL
SyncDbusSessionHelper::InstallPackageNames( const ::sal_uInt32 nXid
, const Sequence
< OUString
>& vPackages
, const OUString
& sInteraction
) throw (RuntimeException
)
71 vector
< OString
> vPackagesOString
;
72 vPackagesOString
.reserve(vPackages
.getLength());
73 boost::shared_ptr
<GVariantBuilder
> pBuilder(g_variant_builder_new(G_VARIANT_TYPE ("as")), GVariantBuilderDeleter());
74 for( const OUString
* pPackage
= stl_begin(vPackages
); pPackage
!= stl_end(vPackages
); ++pPackage
)
76 vPackagesOString
.push_back(OUStringToOString(*pPackage
, RTL_TEXTENCODING_ASCII_US
));
77 g_variant_builder_add(pBuilder
.get(), "s", vPackagesOString
.back().getStr());
80 const OString sInteractionAscii
= OUStringToOString(sInteraction
, RTL_TEXTENCODING_ASCII_US
);
81 boost::shared_ptr
<GDBusProxy
> proxy(lcl_GetPackageKitProxy("Modify"), GObjectDeleter
<GDBusProxy
>());
82 GErrorWrapper
error(NULL
);
83 g_dbus_proxy_call_sync (proxy
.get(),
84 "InstallPackageNames",
85 g_variant_new ("(uass)",
86 sal::static_int_cast
<guint32
>(nXid
),
88 sInteractionAscii
.getStr()),
89 G_DBUS_CALL_FLAGS_NONE
,
91 NULL
, /* cancellable */
95 void SAL_CALL
SyncDbusSessionHelper::IsInstalled( const OUString
& sPackagename
, const OUString
& sInteraction
, ::sal_Bool
& o_isInstalled
) throw (RuntimeException
)
97 const OString sPackagenameAscii
= OUStringToOString(sPackagename
, RTL_TEXTENCODING_ASCII_US
);
98 const OString sInteractionAscii
= OUStringToOString(sInteraction
, RTL_TEXTENCODING_ASCII_US
);
99 boost::shared_ptr
<GDBusProxy
> proxy(lcl_GetPackageKitProxy("Query"), GObjectDeleter
<GDBusProxy
>());
100 GErrorWrapper
error(NULL
);
101 boost::shared_ptr
<GVariant
> result(g_dbus_proxy_call_sync (proxy
.get(),
103 g_variant_new ("(ss)",
104 sPackagenameAscii
.getStr(),
105 sInteractionAscii
.getStr()),
106 G_DBUS_CALL_FLAGS_NONE
,
108 NULL
, /* cancellable */
109 error
.getRef()),GVariantDeleter());
111 o_isInstalled
= g_variant_get_boolean(g_variant_get_child_value(result
.get(),0)) ? sal_True
: sal_False
;
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */