Fcitx
objectvtable_libdbus.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include <memory>
9 #include <mutex>
10 #include <string>
11 #include <utility>
12 #include "../../macros.h"
13 #include "../../stringutils.h"
14 #include "../objectvtable.h"
15 #include "../utils_p.h"
16 #include "bus_p.h"
17 #include "objectvtable_p_libdbus.h"
18 
19 namespace fcitx::dbus {
20 
22 public:
23  bool hasXml_ = false;
24  std::string xml_;
25 };
26 
27 ObjectVTableBasePrivate::~ObjectVTableBasePrivate() {}
28 
29 const std::string &ObjectVTableBasePrivate::getXml(ObjectVTableBase *q) {
30  std::lock_guard<std::mutex> lock(q->privateDataMutexForType());
31  auto *p = q->privateDataForType();
32  if (!p->hasXml_) {
33  p->xml_.clear();
34 
35  for (const auto &m : methods_) {
36  auto *method = m.second;
37  p->xml_ +=
38  stringutils::concat("<method name=\"", method->name(), "\">");
39  for (auto &type : splitDBusSignature(method->signature())) {
40  p->xml_ += stringutils::concat("<arg direction=\"in\" type=\"",
41  type, "\"/>");
42  }
43  for (auto &type : splitDBusSignature(method->ret())) {
44  p->xml_ += stringutils::concat("<arg direction=\"out\" type=\"",
45  type, "\"/>");
46  }
47  p->xml_ += "</method>";
48  }
49 
50  for (const auto &s : sigs_) {
51  auto *sig = s.second;
52  p->xml_ +=
53  stringutils::concat("<signal name=\"", sig->name(), "\">");
54  for (auto &type : splitDBusSignature(sig->signature())) {
55  p->xml_ += stringutils::concat("<arg type=\"", type, "\"/>");
56  }
57  p->xml_ += "</signal>";
58  }
59 
60  for (const auto &pr : properties_) {
61  auto *prop = pr.second;
62  if (prop->writable()) {
63  p->xml_ += stringutils::concat(
64  "<property access=\"readwrite\" type=\"", prop->signature(),
65  "\" name=\"", prop->name(), "\">");
66  } else {
67  p->xml_ += stringutils::concat(
68  "<property access=\"read\" type=\"", prop->signature(),
69  "\" name=\"", prop->name(), "\">");
70  }
71  p->xml_ += "</property>";
72  }
73  p->hasXml_ = true;
74  }
75 
76  return p->xml_;
77 }
78 
79 ObjectVTableBase::ObjectVTableBase()
80  : d_ptr(std::make_unique<ObjectVTableBasePrivate>()) {}
81 
82 ObjectVTableBase::~ObjectVTableBase() {}
83 
84 void ObjectVTableBase::addMethod(ObjectVTableMethod *method) {
85  FCITX_D();
86  d->methods_[method->name()] = method;
87 }
88 
89 void ObjectVTableBase::addProperty(ObjectVTableProperty *property) {
90  FCITX_D();
91  d->properties_[property->name()] = property;
92 }
93 
94 void ObjectVTableBase::addSignal(ObjectVTableSignal *signal) {
95  FCITX_D();
96  d->sigs_[signal->name()] = signal;
97 }
98 
99 ObjectVTableMethod *ObjectVTableBase::findMethod(const std::string &name) {
100  FCITX_D();
101  auto iter = d->methods_.find(name);
102  if (iter == d->methods_.end()) {
103  return nullptr;
104  }
105  return iter->second;
106 }
107 
108 ObjectVTableProperty *ObjectVTableBase::findProperty(const std::string &name) {
109  FCITX_D();
110  auto iter = d->properties_.find(name);
111  if (iter == d->properties_.end()) {
112  return nullptr;
113  }
114  return iter->second;
115 }
116 
117 void ObjectVTableBase::releaseSlot() { setSlot(nullptr); }
118 
119 Bus *ObjectVTableBase::bus() { return std::as_const(*this).bus(); }
120 
121 Bus *ObjectVTableBase::bus() const {
122  FCITX_D();
123  if (d->slot_) {
124  if (auto *bus = d->slot_->bus_.get()) {
125  return bus->bus_;
126  }
127  }
128  return nullptr;
129 }
130 
132  FCITX_D();
133  return !!d->slot_;
134 }
135 
136 const std::string &ObjectVTableBase::path() const {
137  FCITX_D();
138  return d->slot_->path_;
139 }
140 
141 const std::string &ObjectVTableBase::interface() const {
142  FCITX_D();
143  return d->slot_->interface_;
144 }
145 
147  FCITX_D();
148  return d->msg_;
149 }
150 
152  FCITX_D();
153  d->msg_ = message;
154 }
155 
156 std::shared_ptr<ObjectVTablePrivate> ObjectVTableBase::newSharedPrivateData() {
157  return std::make_shared<ObjectVTablePrivate>();
158 }
159 
160 void ObjectVTableBase::setSlot(Slot *slot) {
161  FCITX_D();
162  d->slot_.reset(static_cast<DBusObjectVTableSlot *>(slot));
163 }
164 } // namespace fcitx::dbus
Basic DBus type of a DBus message.
Definition: message.h:224
bool isRegistered() const
Return whether this object is registered to a bus.
Register a DBus read-only property to current DBus VTable.
Definition: objectvtable.h:260
Bus * bus()
Return the bus that the object is registered to.
Virtual base class represent some internal registration of the bus.
Definition: bus.h:33
A class that represents a connection to the Bus.
Definition: bus.h:51
void setCurrentMessage(Message *message)
Set the current dbus message.
void releaseSlot()
Unregister the dbus object from the bus.
const std::string & interface() const
Return the registered dbus interface of the object.
Message * currentMessage() const
Return the current dbus message for current method.
Register a DBus method to current DBus VTable.
Definition: objectvtable.h:75
const std::string & path() const
Return the registered dbus object path of the object.
Register a DBus signal to current DBus VTable.
Definition: objectvtable.h:232