| 1 | #define GI_INLINE 1 |
| 2 | #include <gio/gio.hpp> |
| 3 | |
| 4 | #include <iostream> |
| 5 | |
| 6 | namespace GObject_ = gi::repository::GObject; |
| 7 | namespace GLib = gi::repository::GLib; |
| 8 | namespace Gio = gi::repository::Gio; |
| 9 | |
| 10 | static GLib::MainLoop loop; |
| 11 | |
| 12 | // many calls here support GError |
| 13 | // so typically will throw here instead |
| 14 | // (unless GError output is explicitly requested in call signature) |
| 15 | |
| 16 | // NOTE abundancy of gi::expect not generally needed; |
| 17 | // only needed when using --dl and --expected |
| 18 | |
| 19 | static void |
| 20 | on_reply(GObject_::Object ob, Gio::AsyncResult result) |
| 21 | { |
| 22 | // if not caught here, it will be caught before returning to plain C |
| 23 | #if GI_CONFIG_EXCEPTIONS |
| 24 | try { |
| 25 | #endif |
| 26 | auto connection = gi::object_cast<Gio::DBusConnection>(ob); |
| 27 | |
| 28 | auto call_result = gi::expect(connection.call_finish(result)); |
| 29 | // get single array child |
| 30 | auto names = gi::expect(call_result.get_child_value(0)); |
| 31 | int count = gi::expect(names.n_children()); |
| 32 | |
| 33 | std::cout << count << " message bus names: " << std::endl; |
| 34 | for (int i = 0; i < count; ++i) |
| 35 | std::cout << gi::expect( |
| 36 | gi::expect(names.get_child_value(i)).get_string(nullptr)) |
| 37 | << std::endl; |
| 38 | #if GI_CONFIG_EXCEPTIONS |
| 39 | } catch (const GLib::Error &error) { |
| 40 | std::cerr << "error: '" << error.what() << "'." << std::endl; |
| 41 | } |
| 42 | #endif |
| 43 | |
| 44 | // quit when idle |
| 45 | GLib::idle_add([]() { |
| 46 | loop.quit(); |
| 47 | return GLib::SOURCE_REMOVE_; |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | int |
| 52 | main(int argc, char ** /*argv*/) |
| 53 | { |
| 54 | auto bustype = argc <= 1 ? Gio::BusType::SESSION_ : Gio::BusType::SYSTEM_; |
| 55 | auto connection = gi::expect(Gio::bus_get_sync(bustype)); |
| 56 | |
| 57 | connection.call("org.freedesktop.DBus", "/org/freedesktop/DBus", |
| 58 | "org.freedesktop.DBus", "ListNames", nullptr, nullptr, |
| 59 | Gio::DBusCallFlags::NONE_, -1, nullptr, on_reply); |
| 60 | |
| 61 | loop = gi::expect(GLib::MainLoop::new_()); |
| 62 | loop.run(); |
| 63 | } |