Use giomm DBus interface

This commit is contained in:
Eugen Wissner 2024-06-09 12:17:27 +02:00
parent 9d20748c5c
commit c28c96aec4
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
3 changed files with 61 additions and 41 deletions

View File

@ -1,5 +1,5 @@
find_package(PkgConfig)
pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4)
pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1 glibmm-2.4 giomm-2.4)
add_executable(slack-timedate
slack-timedate.cpp slack-timedate.h

View File

@ -114,40 +114,59 @@ static GVariant *slack_get_property (GDBusConnection *connection, const gchar *s
return NULL;
}
static void on_timedate_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) {
guint registration_id;
GDBusNodeInfo *introspection_data;
GError *slack_err;
const GDBusInterfaceVTable interface_vtable = {
slack_method_call,
slack_get_property,
NULL
};
static void on_timedate_acquired(const Glib::RefPtr<Gio::DBus::Connection>& connection,
const Glib::ustring& name, gpointer user_data)
{
Glib::RefPtr<Gio::DBus::NodeInfo> introspection_data;
static const auto interface_vtable = Gio::DBus::InterfaceVTable(
[user_data](const Glib::RefPtr<Gio::DBus::Connection>& connection,
const Glib::ustring& sender,
const Glib::ustring& obj_path,
const Glib::ustring& interface_name,
const Glib::ustring& method_name,
const Glib::VariantContainerBase& parameters,
const Glib::RefPtr<Gio::DBus::MethodInvocation>& invocation) {
slack_err = NULL;
introspection_data = g_dbus_node_info_new_for_xml (INTROSPECTION_XML, &slack_err);
if (introspection_data == NULL)
g_error ("Failed to parse D-Bus introspection XML: %s\n", slack_err->message);
auto parameters_copy = parameters.gobj_copy();
slack_method_call(connection->gobj(), sender.data(), obj_path.data(), interface_name.data(),
method_name.data(), parameters_copy, invocation->gobj(), user_data);
g_free(parameters_copy);
},
[user_data](Glib::VariantBase& result,
const Glib::RefPtr<Gio::DBus::Connection>& connection,
const Glib::ustring& sender,
const Glib::ustring& obj_path,
const Glib::ustring& interface_name,
const Glib::ustring& prop_name) {
slack_err = NULL;
registration_id = g_dbus_connection_register_object (connection,
BUS_PATH,
introspection_data->interfaces[0],
&interface_vtable,
user_data,
NULL,
&slack_err);
auto property_result = slack_get_property(connection->gobj(), sender.data(), obj_path.data(),
interface_name.data(), prop_name.data(), nullptr, user_data);
result.init(property_result, true);
}
);
if (registration_id <= 0) {
g_critical ("Failed to register callbacks for the exported object with the D-Bus interface: %s\n", slack_err->message);
g_error_free (slack_err);
try
{
introspection_data = Gio::DBus::NodeInfo::create_for_xml(INTROSPECTION_XML);
}
catch (Glib::Error& slack_err)
{
g_error("Failed to parse D-Bus introspection XML: %s\n", slack_err.what().data());
}
try
{
guint registration_id = connection->register_object(BUS_PATH,
introspection_data->lookup_interface(), interface_vtable);
}
catch (Glib::Error& slack_err)
{
g_critical("Failed to register callbacks for the exported object with the D-Bus interface: %s\n",
slack_err.what().data());
}
}
g_dbus_node_info_unref (introspection_data);
}
static void on_timedate_lost (GDBusConnection *connection, const gchar *name, gpointer user_data) {
g_warning ("Failed to acquire the service %s.\n", name);
static void on_timedate_lost(const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& name) {
g_warning("Failed to acquire the service %s.\n", name.data());
exit(1);
}
@ -158,21 +177,21 @@ gboolean timeout_callback (Glib::RefPtr<Glib::MainLoop> loop2quit) {
int main(int argc, char **argv)
{
auto timedate1 = new dlackware::timedate::timedate1();
guint owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
BUS_NAME,
static_cast<GBusNameOwnerFlags>(G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE),
on_timedate_acquired,
NULL,
on_timedate_lost,
timedate1,
[](void *memory) { delete reinterpret_cast<dlackware::timedate::timedate1 *>(memory); });
Gio::init();
auto timedate1 = std::make_unique<dlackware::timedate::timedate1>();
guint owner_id = Gio::DBus::own_name(Gio::DBus::BUS_TYPE_SYSTEM, BUS_NAME,
[&timedate1](const Glib::RefPtr<Gio::DBus::Connection>& connection, const Glib::ustring& name) {
on_timedate_acquired(connection, name, timedate1.get());
}, Gio::DBus::SlotNameAcquired(), &on_timedate_lost,
Gio::DBus::BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | Gio::DBus::BUS_NAME_OWNER_FLAGS_REPLACE);
Glib::RefPtr<Glib::MainLoop> loop = Glib::MainLoop::create(false);
Glib::signal_timeout().connect_seconds(sigc::bind(&timeout_callback, loop), DEFAULT_EXIT_SEC);
loop->run();
g_bus_unown_name (owner_id);
Gio::DBus::unown_name(owner_id);
return EXIT_SUCCESS;
}

View File

@ -18,6 +18,7 @@
#include <dbus/dbus.h>
#include <glibmm.h>
#include <giomm.h>
#include <gio/gio.h>
#define BUS_NAME "org.freedesktop.timedate1"