kodi
DBusUtil.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "DBusMessage.h"
12 #include "utils/Variant.h"
13 
14 #include <memory>
15 #include <string>
16 
17 #include <dbus/dbus.h>
18 
19 class CDBusUtil
20 {
21 public:
22  static CVariant GetAll(const char *destination, const char *object, const char *interface);
23  static CVariant GetVariant(const char *destination, const char *object, const char *interface, const char *property);
27  static bool TryMethodCall(DBusBusType bus, const char* destination, const char* object, const char* interface, const char* method);
31  static bool TryMethodCall(DBusBusType bus, std::string const& destination, std::string const& object, std::string const& interface, std::string const& method);
32 
33 private:
34  static CVariant ParseType(DBusMessageIter *itr);
35  static CVariant ParseVariant(DBusMessageIter *itr);
36 };
37 
39 {
40 public:
42  bool Connect(DBusBusType bus, bool openPrivate = false);
43  bool Connect(DBusBusType bus, CDBusError& error, bool openPrivate = false);
44  void Destroy();
45  operator DBusConnection*();
46 
47 private:
48  CDBusConnection(CDBusConnection const& other) = delete;
49  CDBusConnection& operator=(CDBusConnection const& other) = delete;
50 
51  struct DBusConnectionDeleter
52  {
53  DBusConnectionDeleter() {}
54  bool closeBeforeUnref = false;
55  void operator()(DBusConnection* connection) const;
56  };
57  std::unique_ptr<DBusConnection, DBusConnectionDeleter> m_connection;
58 };
59 
61 {
62 public:
63  CDBusError();
64  ~CDBusError();
65  operator DBusError*();
66  bool IsSet() const;
73  void Reset();
74  // Keep because operator DBusError* would be used for if-statements on
75  // non-const CDBusError instead
76  operator bool();
77  operator bool() const;
78  std::string Name() const;
79  std::string Message() const;
80  void Log(std::string const& message = "DBus error") const;
81  void Log(int level, std::string const& message = "DBus error") const;
82 
83 private:
84  CDBusError(CDBusError const& other) = delete;
85  CDBusError& operator=(CDBusError const& other) = delete;
86 
87  DBusError m_error;
88 };
Definition: DBusUtil.h:38
Definition: DBusUtil.h:19
static bool TryMethodCall(DBusBusType bus, const char *destination, const char *object, const char *interface, const char *method)
Try to call a DBus method and return whether the call succeeded.
Definition: DBusUtil.cpp:158
Definition: Variant.h:31
Definition: DBusUtil.h:60