[connman] Share a single connection to D-Bus system bus.
[profile/ivi/settings-daemon.git] / plugins / connman / dbus_connection.cpp
1 /**
2  * @file dbus_connection.cpp
3  *
4  * @brief Settings daemon D-Bus connection adapter.
5  *
6  * @author Michael Leibowitz @<michael.leibowitz@@intel.com@>
7  * @author Ossama Othman @<ossama.othman@@intel.com@>
8  *
9  * @copyright @par
10  * Copyright 2013 Intel Corporation All Rights Reserved.
11  * @par
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation;
15  * version 2.1 of the License.
16  * @par
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  * @par
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  * Boston, MA  02110-1301  USA
26  */
27
28 #include "dbus_connection.hpp"
29
30 #include <settingsd/glib_traits.hpp>
31 #include <settingsd/unique_ptr.hpp>
32
33 #include <stdexcept>
34
35
36 ivi::settings::dbus_connection::dbus_connection()
37   : connection_(nullptr)
38 {
39   GError * error = nullptr;
40
41   connection_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
42
43   if (connection_ == nullptr) {
44     unique_ptr<GError> safe_error(error);
45     throw std::runtime_error(error->message);
46   }
47 }
48
49 ivi::settings::dbus_connection::~dbus_connection()
50 {
51   GError * error = nullptr;
52
53   if (connection_ != nullptr
54       && !g_dbus_connection_close_sync(connection_,
55                                        nullptr,
56                                        &error)) {
57     g_critical("D-Bus connection close failed: %s.\n",
58                error->message);
59     g_error_free(error);
60   }
61 }
62
63
64 // Local Variables:
65 // mode:c++
66 // c-basic-offset:2
67 // indent-tabs-mode: nil
68 // End: