1// Copyright (c) Benjamin Kietzman (github.com/bkietz)
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef DBUS_CONNECTION_IPP
7#define DBUS_CONNECTION_IPP
8
9#include <dbus/dbus.h>
10#include <dbus/detail/watch_timeout.hpp>
11
12#include <boost/intrusive_ptr.hpp>
13
14void intrusive_ptr_add_ref(DBusConnection *c)
15{
16  dbus_connection_ref(c);
17}
18
19void intrusive_ptr_release(DBusConnection *c)
20{
21  dbus_connection_unref(c);
22}
23
24namespace dbus {
25namespace impl {
26
27class connection
28{
29public:
30  boost::intrusive_ptr<DBusConnection> conn;
31  bool is_shared;
32  bool is_paused;
33
34  connection()
35  {
36  }
37
38  connection(boost::asio::io_service& io, int bus, bool shared)
39    : is_shared(shared),
40      is_paused(true)
41  {
42    error e;
43    if(is_shared)
44    {
45      conn = dbus_bus_get((DBusBusType)bus, e);
46    }
47    else
48    {
49      conn = dbus_bus_get_private((DBusBusType)bus, e);
50    }
51
52    e.throw_if_set();
53
54    dbus_connection_set_exit_on_disconnect(conn.get(), false);
55
56    detail::set_watch_timeout_dispatch_functions(conn.get(), io);
57  }
58
59  connection(boost::asio::io_service& io, const string& address, bool shared)
60    : is_shared(shared),
61      is_paused(true)
62  {
63    error e;
64    if(shared)
65    {
66      conn = dbus_connection_open(address.c_str(), e);
67    }
68    else
69    {
70      conn = dbus_connection_open_private(address.c_str(), e);
71    }
72
73    e.throw_if_set();
74
75    dbus_connection_set_exit_on_disconnect(conn.get(), false);
76
77    detail::set_watch_timeout_dispatch_functions(conn.get(), io);
78  }
79
80  ~connection()
81  {
82    if(!is_shared) dbus_connection_close(conn.get());
83  }
84
85  operator DBusConnection *()
86  {
87    return conn.get();
88  }
89  operator const DBusConnection *() const
90  {
91    return conn.get();
92  }
93
94  void start()
95  {
96  }
97};
98
99} // namespace impl
100} // namespace dbus
101
102#endif // DBUS_CONNECTION_IPP
103