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) 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 add_executable(slack-timedate
slack-timedate.cpp slack-timedate.h slack-timedate.cpp slack-timedate.h

View File

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

View File

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