1 #ifndef _DBUSSIGNALLER_H_
2 #define _DBUSSIGNALLER_H_
9 #include <unordered_map>
12 #include "varianttype.h"
13 #include "superptr.hpp"
14 #include "listplusplus.h"
19 DBusSignal():connection(nullptr), property(nullptr){}
20 DBusSignal(GDBusConnection* conn, const std::string & objPath, const std::string & iface, const std::string & sigName, VariantType* var)
21 : connection(conn), objectPath(objPath), interface(iface), signalName(sigName), property(var)
31 bool operator == (const DBusSignal &other) const
33 return connection == other.connection &&
34 objectPath == other.objectPath &&
35 interface == other.interface &&
36 signalName == other.signalName &&
37 property == other.property;
40 GDBusConnection* connection;
41 std::string objectPath;
42 std::string interface;
43 std::string signalName;
44 VariantType* property;
51 static DBusSignaller* factory(int timeout)
54 return singleton = new DBusSignaller(timeout);
58 void fireSignal(GDBusConnection* conn, const std::string & objPath, const std::string & iface,
59 const std::string & sigName, VariantType* prop)
61 DBusSignal * signal = new DBusSignal(conn, objPath, iface, sigName, prop);
63 if(queue.find(objPath) != queue.end())
66 for(auto i : queue[objPath])
76 queue[objPath].push_back(signal);
80 queue[objPath].push_back(signal);
88 DBusSignaller(int timeout)
90 g_timeout_add(timeout,[](gpointer userData)
92 std::unordered_map<std::string, std::vector<DBusSignal*>> *q = static_cast<std::unordered_map<std::string, std::vector<DBusSignal*>>*>(userData);
93 std::unordered_map<std::string, std::vector<DBusSignal*>> queue = *q;
97 std::string objectPath;
98 GDBusConnection* connection;
99 std::string interfaceName;
100 std::string signalName;
102 std::unordered_map<std::string, GVariant*> variantMap;
104 for(auto s : itr.second)
106 std::unique_ptr<DBusSignal> signal(s);
107 objectPath = signal->objectPath;
108 connection = signal->connection;
109 interfaceName = signal->interface;
110 signalName = signal->signalName;
112 VariantType* property = signal->property;
114 auto val = g_variant_ref(property->toVariant());
115 std::string sequenceName = property->name() + "Sequence";
117 variantMap[property->name()] = val;
118 variantMap[sequenceName] = g_variant_new("i", property->sequence());
119 variantMap["Time"] = g_variant_new("d", property->timestamp());
120 variantMap["Zone"] = g_variant_new("i", property->zoneFilter());
123 GVariantBuilder builder;
124 g_variant_builder_init(&builder, G_VARIANT_TYPE_DICTIONARY);
126 for(auto sv : variantMap)
128 /// Send PropertiesChanged signal
130 auto value = sv.second;
132 g_variant_builder_add(&builder, "{sv}", key.c_str(), value);
135 GError* error = nullptr;
137 g_dbus_connection_emit_signal(connection, NULL, objectPath.c_str(),
138 "org.freedesktop.DBus.Properties",
140 g_variant_new("(sa{sv}as)", interfaceName.c_str(), &builder, NULL),
143 auto errorPtr = amb::make_super(error);
147 DebugOut(DebugOut::Error)<<errorPtr->message<<std::endl;
158 std::unordered_map<std::string, std::vector<DBusSignal*>> queue;
160 static DBusSignaller * singleton;