summaryrefslogtreecommitdiff
path: root/examples/dbus/well-known-address-client.cc
blob: 43977b79cd1d43f3f3c5cf903613d240c132cc5c (plain)
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
/* 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 main_loop_idle()
{
  loop->quit();
  return false;
}

// A callback to finish creating a DBusProxy that was asynchronously created
// for the user session's bus and then try to call the well known 'ListNames'
// method.
void dbus_proxy_available(Glib::RefPtr<Gio::AsyncResult>& result)
{
  Glib::RefPtr<Gio::DBusProxy> proxy = Gio::DBusProxy::create_finish(result);

  if(!proxy)
  {
    std::cerr << "The proxy to the user's session bus was not successfully "
      "created." << std::endl;
    loop->quit();
    return;
  }

  // Call the 'ListNames' method and print the results.

  // 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(&main_loop_idle));
}

int main(int, char**)
{
  std::locale::global(std::locale(""));
  Gio::init();

  loop = Glib::MainLoop::create();

  // Get the user session bus connection.
  Glib::RefPtr<Gio::DBusConnection> connection =
    Gio::DBusConnection::get_sync(Gio::BUS_TYPE_SESSION);

  // Check for an unavailable connection.
  if (!connection)
  {
    std::cerr << "The user's session bus is not available." << std::endl;
    return 1;
  }

  // Print out the unique name of the connection to the user session bus.
  std::cout << connection->get_unique_name() << std::endl;

  // Create the proxy to the bus asynchronously.
  Gio::DBusProxy::create(connection, "org.freedesktop.DBus",
    "/org/freedesktop/DBus", "org.freedesktop.DBus",
    sigc::ptr_fun(&dbus_proxy_available));

  loop->run();

  return 0;
}