Files
tdesktop/cmake/external/glib/cppgir/examples/gio-dbus-client.cpp
allhaileris afb81b8278
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
init
2026-02-16 15:50:16 +03:00

64 lines
1.8 KiB
C++

#define GI_INLINE 1
#include <gio/gio.hpp>
#include <iostream>
namespace GObject_ = gi::repository::GObject;
namespace GLib = gi::repository::GLib;
namespace Gio = gi::repository::Gio;
static GLib::MainLoop loop;
// many calls here support GError
// so typically will throw here instead
// (unless GError output is explicitly requested in call signature)
// NOTE abundancy of gi::expect not generally needed;
// only needed when using --dl and --expected
static void
on_reply(GObject_::Object ob, Gio::AsyncResult result)
{
// if not caught here, it will be caught before returning to plain C
#if GI_CONFIG_EXCEPTIONS
try {
#endif
auto connection = gi::object_cast<Gio::DBusConnection>(ob);
auto call_result = gi::expect(connection.call_finish(result));
// get single array child
auto names = gi::expect(call_result.get_child_value(0));
int count = gi::expect(names.n_children());
std::cout << count << " message bus names: " << std::endl;
for (int i = 0; i < count; ++i)
std::cout << gi::expect(
gi::expect(names.get_child_value(i)).get_string(nullptr))
<< std::endl;
#if GI_CONFIG_EXCEPTIONS
} catch (const GLib::Error &error) {
std::cerr << "error: '" << error.what() << "'." << std::endl;
}
#endif
// quit when idle
GLib::idle_add([]() {
loop.quit();
return GLib::SOURCE_REMOVE_;
});
}
int
main(int argc, char ** /*argv*/)
{
auto bustype = argc <= 1 ? Gio::BusType::SESSION_ : Gio::BusType::SYSTEM_;
auto connection = gi::expect(Gio::bus_get_sync(bustype));
connection.call("org.freedesktop.DBus", "/org/freedesktop/DBus",
"org.freedesktop.DBus", "ListNames", nullptr, nullptr,
Gio::DBusCallFlags::NONE_, -1, nullptr, on_reply);
loop = gi::expect(GLib::MainLoop::new_());
loop.run();
}