1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/* Copyright (C) 2010 The giomm Development Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <giomm.h>
#include <glibmm.h>
#include <iostream>
// The main loop.
Glib::RefPtr<Glib::MainLoop> loop;
// A main loop idle callback to quit when the main loop is idle.
bool on_main_loop_idle()
{
loop->quit();
return false;
}
// A callback to finish creating a DBus::Proxy that was asynchronously created
// for the user session's bus and then try to call the well known 'ListNames'
// method.
void on_dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
{
const auto proxy = Gio::DBus::Proxy::create_finish(result);
if(!proxy)
{
std::cerr << "The proxy to the user's session bus was not successfully "
"created." << std::endl;
loop->quit();
return;
}
try
{
// The proxy's call method returns a tuple of the value(s) that the method
// call produces so just get the tuple as a VariantContainerBase.
const auto call_result = proxy->call_sync("ListNames");
// Now extract the single item in the variant container which is the
// array of strings (the names).
Glib::Variant< std::vector<Glib::ustring>> names_variant;
call_result.get_child(names_variant);
// Get the vector of strings.
auto names = names_variant.get();
std::cout << "The names on the message bus are:" << std::endl;
for(const auto& i : names)
std::cout << i << "." << std::endl;
}
catch(const Glib::Error& error)
{
std::cerr << "Got an error: '" << error.what() << "'." << std::endl;
}
// Connect an idle callback to the main loop to quit when the main loop is
// idle now that the method call is finished.
Glib::signal_idle().connect(sigc::ptr_fun(&on_main_loop_idle));
}
int main(int, char**)
{
std::locale::global(std::locale(""));
Gio::init();
loop = Glib::MainLoop::create();
// Get the user session bus connection.
auto connection =
Gio::DBus::Connection::get_sync(Gio::DBus::BUS_TYPE_SESSION);
// Check for an unavailable connection.
if(!connection)
{
std::cerr << "The user's session bus is not available." << std::endl;
return 1;
}
// Create the proxy to the bus asynchronously.
Gio::DBus::Proxy::create(connection, "org.freedesktop.DBus",
"/org/freedesktop/DBus", "org.freedesktop.DBus",
sigc::ptr_fun(&on_dbus_proxy_available));
loop->run();
return 0;
}
|