1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "syncableobject.h"
24 #include <QMetaProperty>
26 #include "signalproxy.h"
29 SyncableObject::SyncableObject(QObject* parent)
30 : SyncableObject(QString{}, parent)
33 SyncableObject::SyncableObject(const QString& objectName, QObject* parent)
36 _objectName = objectName;
37 setObjectName(objectName);
39 connect(this, &QObject::objectNameChanged, this, [this](auto&& newName) {
40 for (auto&& proxy : _signalProxies) {
41 proxy->renameObject(this, newName, _objectName);
43 _objectName = newName;
47 SyncableObject::SyncableObject(const SyncableObject& other, QObject* parent)
48 : SyncableObject(QString{}, parent)
50 _initialized = other._initialized;
51 _allowClientUpdates = other._allowClientUpdates;
54 SyncableObject::~SyncableObject()
56 QList<SignalProxy*>::iterator proxyIter = _signalProxies.begin();
57 while (proxyIter != _signalProxies.end()) {
58 SignalProxy* proxy = (*proxyIter);
59 proxyIter = _signalProxies.erase(proxyIter);
60 proxy->stopSynchronize(this);
64 SyncableObject& SyncableObject::operator=(const SyncableObject& other)
69 _initialized = other._initialized;
70 _allowClientUpdates = other._allowClientUpdates;
74 bool SyncableObject::isInitialized() const
79 void SyncableObject::setInitialized()
85 QVariantMap SyncableObject::toVariantMap()
87 QVariantMap properties;
89 const QMetaObject* meta = metaObject();
91 // we collect data from properties
94 for (int i = 0; i < meta->propertyCount(); i++) {
95 prop = meta->property(i);
96 propName = QString(prop.name());
97 if (propName == "objectName")
99 properties[propName] = prop.read(this);
102 // ...as well as methods, which have names starting with "init"
103 for (int i = 0; i < meta->methodCount(); i++) {
104 QMetaMethod method = meta->method(i);
105 QString methodname(SignalProxy::ExtendedMetaObject::methodName(method));
106 if (!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
109 QVariant::Type variantType = QVariant::nameToType(method.typeName());
110 if (variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
111 qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.methodSignature()
112 << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
116 QVariant value(variantType, (const void*)nullptr);
117 QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), value.data());
118 QMetaObject::invokeMethod(this, methodname.toLatin1(), genericvalue);
120 properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
125 void SyncableObject::fromVariantMap(const QVariantMap& properties)
127 const QMetaObject* meta = metaObject();
129 QVariantMap::const_iterator iterator = properties.constBegin();
131 while (iterator != properties.constEnd()) {
132 propName = iterator.key();
133 if (propName == "objectName") {
138 int propertyIndex = meta->indexOfProperty(propName.toLatin1());
140 if (propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
141 setInitValue(propName, iterator.value());
143 setProperty(propName.toLatin1(), iterator.value());
144 // qDebug() << "<<< SYNC:" << name << iterator.value();
149 bool SyncableObject::setInitValue(const QString& property, const QVariant& value)
151 QString handlername = QString("initSet") + property;
152 handlername[7] = handlername[7].toUpper();
154 QString methodSignature = QString("%1(%2)").arg(handlername).arg(value.typeName());
155 int methodIdx = metaObject()->indexOfMethod(methodSignature.toLatin1().constData());
157 QByteArray normedMethodName = QMetaObject::normalizedSignature(methodSignature.toLatin1().constData());
158 methodIdx = metaObject()->indexOfMethod(normedMethodName.constData());
164 QGenericArgument param(value.typeName(), value.constData());
165 return QMetaObject::invokeMethod(this, handlername.toLatin1(), param);
168 void SyncableObject::update(const QVariantMap& properties)
170 fromVariantMap(properties);
171 SYNC(ARG(properties))
175 void SyncableObject::requestUpdate(const QVariantMap& properties)
177 if (allowClientUpdates()) {
180 REQUEST(ARG(properties))
183 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char* funcname, ...) const
185 // qDebug() << Q_FUNC_INFO << modeType << funcname;
186 foreach (SignalProxy* proxy, _signalProxies) {
188 va_start(ap, funcname);
189 proxy->sync_call__(this, modeType, funcname, ap);
194 void SyncableObject::synchronize(SignalProxy* proxy)
196 if (_signalProxies.contains(proxy))
198 _signalProxies << proxy;
201 void SyncableObject::stopSynchronize(SignalProxy* proxy)
203 for (int i = 0; i < _signalProxies.count(); i++) {
204 if (_signalProxies[i] == proxy) {
205 _signalProxies.removeAt(i);